/**
 * Copyright (c) 2011-2012
 * OmniCore CodeGuide
 */

package de.tum.in.oose.bumpers.cars;

import java.awt.Image;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import de.tum.in.oose.bumpers.cars.instruments.Instrument;
import de.tum.in.oose.bumpers.ui.InstrumentPanel;

/**
 * The User Car impl. 
 */
public class UserCar extends Car {
    private static Image defaultImage;

    /**
	 * @return the defaultImage
	 */
	public static Image getDefaultImage() {
		return defaultImage;
	}

	/**
	 * @param defaultImage the defaultImage to set
	 */
	public static void setDefaultImage(Image defaultImage) {
		UserCar.defaultImage = defaultImage;
	}

	private ArrayList<Instrument> instruments = new ArrayList<Instrument>();

    private InstrumentPanel instrumentPanel;

	/**
	 * standard constructor for a user car .
	 */
    public UserCar() {
        super();
        instrumentPanel = new InstrumentPanel(this);
    }

	/**
	 * @param maxX -
	 * @param maxY -
	 */
    public void initiate(int maxX, int maxY) {
        setBody(defaultImage);
        this.setPosition(new Point(0, maxY));
        setDirection(90);
        this.setSpeed(5);
        notifyInstruments();
    }

	/**
	 * @param component that will be subscribed to the instrument 
	 */
    public void subscribeInstrument(Instrument component) {
        if (component != null && !this.instruments.contains(component)) {
            this.instruments.add(component);
        }
    }
	/**
	 * @param instrument that will be unsubscribed from the instrument 
	 */
    public void unsubscribeInstrument(Instrument instrument) {
        if (instrument != null && this.instruments.contains(instrument)) {
            this.instruments.remove(instrument);
        }
    }

	/**
	 * Notify instruments to redraw. 
	 */
    public synchronized void notifyInstruments() {
        Iterator<Instrument> iter = instruments.iterator();
        while (iter.hasNext()) {
            Instrument aComponent = iter.next();
            aComponent.updateInstrument();
        }
    }

	/**
	 * @return list of instruments 
	 */
    public List<Instrument> getInstruments() {
        return instruments;
    }

	/**
	 * @param maxX -
	 * @param maxY -
	 */
    public synchronized void updatePosition(int maxX, int maxY) {
        super.updatePosition(maxX, maxY);
        notifyInstruments();
    }

	/**
	 * @return the instr panel.
	 */
    public InstrumentPanel getInstrumentPanel() {
        return this.instrumentPanel;
    }
}
